package com.x.erp.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.x.erp.model.Car;
import com.x.erp.model.Contract;
import com.x.erp.model.Market;
import com.x.erp.model.Shop;
import com.x.erp.model.statis.MarketStorageVO;
import com.x.erp.rpc.*;
import com.x.erp.util.ConstantState;
import com.x.framework.Base;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;
import org.apache.commons.lang.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/public/tvStatis")
public class TVStatisController extends BaseController {

	@Reference
	private TVStatisRpcService tvStatisRpcService;// 电视统计

	@Reference
	private CarRpcService carRpcService;// 车辆服务

	@Reference
	private ShopRpcService shopRpcService;// 车商服务

	@Reference
	private SaleBillRpcService saleBillRpcService;// 销售单据服务

	@Reference
	private MarketRpcService marketRpcService;// 市场服务

	@Reference
	private ContractRpcService contractRpcService;// 合同服务

	/**
	 * 获取市场入库信息
	 * @param marketStorageVO
	 * @return
	 */
	@RequestMapping("/getMarketStorageInfo")
	public ResultModel getMarketStorageInfo(MarketStorageVO marketStorageVO){
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", tvStatisRpcService.searchMarketStorageInfo(marketStorageVO));
	}

	/**
	 * 获取工作台统计信息
	 * @param car
	 * @param marketStorageVO
	 * @return
	 */
	@RequestMapping("/getWorkbenchInfo")
	public ResultModel getWorkbenchInfo(Car car, MarketStorageVO marketStorageVO){
		if(StringUtils.isEmpty(car.getMarketId())){
			return new ResultModel(ResultCode.RESULT_FAIL, "市场ID不能为空！");
		}
		Map<String, Object> res = new HashMap<>();
		//当前库存
		int carStock = tvStatisRpcService.searchCarStock(marketStorageVO);
		res.put("carStock", carStock);
		//上架数量
		marketStorageVO.setCarSaleState(ConstantState.CAR_SALE_STATE_ONSALE_FINISH);
		int carPutOnSaleNum = tvStatisRpcService.searchCarStock(marketStorageVO);
		res.put("carPutOnSaleNum", carPutOnSaleNum);
		//平台商户数
		if(marketStorageVO.getShopId() == null) {
			Shop shop = new Shop();
			if (marketStorageVO.getMarketId().equals("1")) {
				// 查询合作市场数
				Market market = new Market();
				int marketNum = marketRpcService.selectMarketNum(market);
				res.put("marketNum", marketNum);
			} else {
				// 查询有效合同数
				Contract contract = new Contract();
				contract.setMarketId(car.getMarketId());
				int contractNum = contractRpcService.selectContractNum(contract);
				res.put("contractNum", contractNum);
				shop.setMarketId(marketStorageVO.getMarketId());
			}
			//商户数
			int shopNum = shopRpcService.selectShopNum(shop);
			res.put("shopNum", shopNum);
			//质押车辆数
			car.setCarPledgeState(ConstantState.CAR_PLEDG_STATE_PLEDGED);
			int pledgeCarNum = carRpcService.selectCarNum(car);
			res.put("pledgeCarNum", pledgeCarNum);
			//质押商户数
			int pledgeShopNum = shopRpcService.selectPledgeShopNum(shop);
			res.put("pledgeShopNum", pledgeShopNum);
		} else {
			car.setCarStockState(ConstantState.CAR_STOCK_STATE_INITIAL);
			res.put("stockStateInitialNum", carRpcService.selectCarNum(car));
			car.setCarStockState(null);
		}
		//本月交易量和交易额
		Map<String, Object> totalOutStorageInfo = carRpcService.selectCarOutStockInfo(car, null, null);
		res.put("totalOutStorageInfo", totalOutStorageInfo);
		//上月月销量
		Date thisMonth = this.getDate(getCurrentDate(0, "yyyy-MM"), "yyyy-MM");//本月
		Date lastMonth = this.getDate(this.getDate(getDate(thisMonth, -1), "yyyy-MM"), "yyyy-MM");//上月
		Date cDate = this.getDate(0);
		marketStorageVO.setCarSaleState(null);
		//本月交易量和交易额
		marketStorageVO.setStartDate(thisMonth);
		Map<String, Object> thisMonthOutStorageInfo = carRpcService.selectCarOutStockInfo(car, thisMonth, cDate);
		res.put("thisMonthOutStorageInfo", thisMonthOutStorageInfo);
		//本月交易量和交易额
		marketStorageVO.setStartDate(lastMonth);
		Map<String, Object> lastMonthOutStorageInfo = carRpcService.selectCarOutStockInfo(car, lastMonth, thisMonth);
		res.put("lastMonthOutStorageInfo", lastMonthOutStorageInfo);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	/**
	 * 查询概要统计
	 * @param car
	 * @return
	 */
	@RequestMapping(value = "/getOutline")
	public ResultModel getOutline(Car car, MarketStorageVO marketStorageVO) {
		Map<String, Object> res = new HashMap<>();
		Date today = this.getDate(Base.getCurrentDate(0, "yyyy-MM-dd"), "yyyy-MM-dd");//今天
		Date thisMonth = this.getDate(getCurrentDate(0, "yyyy-MM"), "yyyy-MM");//本月
		Date cDate = this.getDate(0);
		//今日入库量
		Map<String, Object> todayStorageInfo = carRpcService.selectCarStorageInfo(car, today, cDate);
		//本月入库量
		Map<String, Object> thisMonthStorageInfo = carRpcService.selectCarStorageInfo(car, thisMonth, cDate);
		//累计入库量
		Map<String, Object> totalStorageInfo = carRpcService.selectCarStorageInfo(car, null, cDate);
		//当前库存
		int carStock = tvStatisRpcService.searchCarStock(marketStorageVO);
		//今日出库量
		Map<String, Object> todayOutStorageInfo = carRpcService.selectCarOutStockInfo(car, today, cDate);
		//本月出库量
		Map<String, Object> thisMonthOutStorageInfo = carRpcService.selectCarOutStockInfo(car, thisMonth, cDate);
		//累计出库量
		Map<String, Object> totalOutStorageInfo = carRpcService.selectCarOutStockInfo(car, null, cDate);
		//库存预订量
		Map<String, Object> carOrderInfo = carRpcService.selectCarOrderInfo(car, null, cDate);
		//累计交易额
		marketStorageVO.setEndDate(cDate);
		MarketStorageVO totalSaleVolume = tvStatisRpcService.searchCarSaleVolume(marketStorageVO);
		//本月交易额
		marketStorageVO.setStartDate(thisMonth);
		MarketStorageVO thisMonthSaleVolume = tvStatisRpcService.searchCarSaleVolume(marketStorageVO);
		//平台商户数
		Shop shop = new Shop();
		if(StringUtils.isNotEmpty(car.getMarketId()) && !car.getMarketId().equals("1")){
			shop.setMarketId(car.getMarketId());
		}
		int shopNum = shopRpcService.selectShopNum(shop);
		//上架数量
		marketStorageVO.setCarSaleState(ConstantState.CAR_SALE_STATE_ONSALE_FINISH);
		marketStorageVO.setEndDate(null);
		marketStorageVO.setStartDate(null);
		int carPutOnSaleNum = tvStatisRpcService.searchCarStock(marketStorageVO);
		res.put("todayStorageInfo", todayStorageInfo);
		res.put("thisMonthStorageInfo", thisMonthStorageInfo);
		res.put("totalStorageInfo", totalStorageInfo);
		res.put("carStock", carStock);
		res.put("todayOutStorageInfo", todayOutStorageInfo);
		res.put("thisMonthOutStorageInfo", thisMonthOutStorageInfo);
		res.put("totalOutStorageInfo", totalOutStorageInfo);
		res.put("carOrderInfo", carOrderInfo);
		res.put("totalSaleVolume", totalSaleVolume);
		res.put("thisMonthSaleVolume", thisMonthSaleVolume);
		res.put("shopNum", shopNum);
		res.put("carPutOnSaleNum", carPutOnSaleNum);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	/**
	 * 查询出库、入库统计
	 * @param marketStorageVO
	 * @return
	 */
	@RequestMapping(value = "/getMarketStorageOutAndIn")
	public ResultModel getMarketStorageOutAndIn(MarketStorageVO marketStorageVO, Car car) {
		Map<String, Object> res = new HashMap<>();
		List<Map<String, Object>> storageInfo = carRpcService.selectMarketCarStorageInfo(car, null, null);
		List<MarketStorageVO> outStorageInfo = tvStatisRpcService.searchCarOutStorageInfo(marketStorageVO);
		res.put("storageInfo", storageInfo);
		res.put("outStorageInfo", outStorageInfo);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	/**
	 * 查询每日出库、入库统计
	 * @param marketStorageVO
	 * @return
	 */
	@RequestMapping(value = "/getDayStorageOutAndIn")
	public ResultModel getDayStorageOutAndIn(MarketStorageVO marketStorageVO, Integer dayNum) {
		Map<String, Object> res = new HashMap<>();
		if (!createMarketStorageVOStartAndEndDate(marketStorageVO, dayNum)) {
			return new ResultModel(ResultCode.RESULT_FAIL, "起止日期不能为空！");
		}
		List<MarketStorageVO> storageInfo = tvStatisRpcService.searchDayCarStorageInfo(marketStorageVO);
		List<MarketStorageVO> outStorageInfo = tvStatisRpcService.searchDayCarOutStorageInfo(marketStorageVO);
		res.put("storageInfo", storageInfo);
		res.put("outStorageInfo", outStorageInfo);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	/**
	 * 查询每日销售额统计
	 * @param marketStorageVO
	 * @return
	 */
	@RequestMapping(value = "/getDaySaleInfo")
	public ResultModel getDaySaleInfo(MarketStorageVO marketStorageVO, Integer dayNum) {
		Map<String, Object> res = new HashMap<>();
		if (!createMarketStorageVOStartAndEndDate(marketStorageVO, dayNum)) {
			return new ResultModel(ResultCode.RESULT_FAIL, "起止日期不能为空！");
		}
		List<MarketStorageVO> saleVolumeInfo = tvStatisRpcService.searchDaySaleVolume(marketStorageVO);
		res.put("saleVolumeInfo", saleVolumeInfo);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	// 设置查询的起止日期
	private boolean createMarketStorageVOStartAndEndDate(MarketStorageVO marketStorageVO, Integer dayNum) {
		dayNum = dayNum == null?30:dayNum;
		if(dayNum == 0){
			if(marketStorageVO.getStartDate() == null || marketStorageVO.getEndDate() == null){
				return false;
			}
		}else{
			marketStorageVO.setStartDate((this.getDate(this.getCurrentDate(-dayNum, "yyyy-MM-dd"), "yyyy-MM-dd")));
			marketStorageVO.setEndDate(this.getDate(1));
		}
		return true;
	}

	/**
	 * 查询入库车辆列表
	 * @param marketStorageVO
	 * @return
	 */
	@RequestMapping(value = "/getCarList")
	public ResultModel getCarList(MarketStorageVO marketStorageVO, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date lastDate){
		marketStorageVO.setEndDate(lastDate);
		List<Car> cars = tvStatisRpcService.searchCarList(marketStorageVO);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", cars);
	}

	/**
	 * 查询出库车辆列表
	 * @param marketStorageVO
	 * @return
	 */
	@RequestMapping(value = "/getCarSaleList")
	public ResultModel getCarSaleList(MarketStorageVO marketStorageVO, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date lastDate){
		marketStorageVO.setEndDate(lastDate);
		List<Car> cars = tvStatisRpcService.searchCarSaleList(marketStorageVO);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", cars);
	}

	/**
	 * 查询品牌排行
	 * @param car
	 * @return
	 */
	@RequestMapping(value = "/getBrandRank")
	public ResultModel getBrandRank(MarketStorageVO marketStorageVO, Car car){
		if(car.getPageSize() == null){
			car.setPageSize(10);
			marketStorageVO.setPageSize(10);
		}
		Map<String, Object> res = new HashMap<>();
		//品牌排行
		List<MarketStorageVO> storageBrandRank = tvStatisRpcService.searchStockBrandRank(marketStorageVO);
		List<Map<String, Object>> carOutBrandRank = carRpcService.selectCarOutStockBrandRank(car, null, null);
		res.put("storageBrandRank", storageBrandRank);
		res.put("carOutBrandRank", carOutBrandRank);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	/**
	 * 查询颜色排行
	 * @param marketStorageVO
	 * @return
	 */
	@RequestMapping(value = "/getColorRank")
	public ResultModel getColorRank(MarketStorageVO marketStorageVO){
		if(marketStorageVO.getPageSize() == null){
			marketStorageVO.setPageSize(10);
		}
		Map<String, Object> res = new HashMap<>();
		//颜色排行
		List<MarketStorageVO> storageColorRank = tvStatisRpcService.searchStorageColorRank(marketStorageVO);
		List<MarketStorageVO> carOutColorRank = tvStatisRpcService.searchCarOutStockColorRank(marketStorageVO);
		res.put("storageColorRank", storageColorRank);
		res.put("carOutColorRank", carOutColorRank);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	/**
	 * 查询价格排行
	 * @param marketStorageVO
	 * @return
	 */
	@RequestMapping(value = "/getPriceRank")
	public ResultModel getPriceRank(MarketStorageVO marketStorageVO){
		Map<String, Object> res = new HashMap<>();
		//价格排行
		List<MarketStorageVO> carOutPriceRank = tvStatisRpcService.searchCarOutStockPriceRank(marketStorageVO);
		res.put("carOutPriceRank", carOutPriceRank);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	/**
	 * 查询销售出库量排行
	 * @param marketStorageVO
	 * @return
	 */
	@RequestMapping(value = "/getCarOutRank")
	public ResultModel getCarOutRank(MarketStorageVO marketStorageVO){
		Map<String, Object> res = new HashMap<>();
		//销售出库量排行
		List<MarketStorageVO> carOutRank = tvStatisRpcService.searchCarOutStockRank(marketStorageVO);
		res.put("carOutRank", carOutRank);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	/**
	 * 查询销售额排行
	 * @param marketStorageVO
	 * @return
	 */
	@RequestMapping(value = "/getCarSaleVolumeRank")
	public ResultModel getCarSaleVolumeRank(MarketStorageVO marketStorageVO){
		Map<String, Object> res = new HashMap<>();
		//销售额排行
		List<MarketStorageVO> carSaleVolumeRank = tvStatisRpcService.searchCarSaleVolumeRank(marketStorageVO);
		res.put("carSaleVolumeRank", carSaleVolumeRank);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	/**
	 * 查询车系排行
	 * @param car
	 * @return
	 */
	@RequestMapping(value = "/getMonthSeriesRank")
	public ResultModel getMonthSeriesRank(Car car){
		if(car.getPageSize() == null){
			car.setPageSize(10);
		}
		Date thisMonth = this.getDate(-30);//本月
		Date cDate = this.getDate(0);
		Map<String, Object> res = new HashMap<>();
		//本月车系排行
		List<Map<String, Object>> carOutSeriesRank = carRpcService.selectCarOutStockSeriesRank(car, thisMonth, cDate);
		res.put("carOutSeriesRank", carOutSeriesRank);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	/**
	 * 查询排量排行
	 * @param marketStorageVO
	 * @return
	 */
	@RequestMapping(value = "/getDisplacementRank")
	public ResultModel getDisplacementRank(MarketStorageVO marketStorageVO){
		Map<String, Object> res = new HashMap<>();
		//查询排量排行
		List<MarketStorageVO> carDisplacementRank = tvStatisRpcService.searchCarDisplacementRank(marketStorageVO);
		res.put("carDisplacementRank", carDisplacementRank);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	/**
	 * 查询初检信息
	 * @param marketStorageVO
	 * @return
	 */
	@RequestMapping(value = "/getCarFirstDetectInfo")
	public ResultModel getCarFirstDetectInfo(MarketStorageVO marketStorageVO){
		Map<String, Object> res = new HashMap<>();
		//查询初检信息
		marketStorageVO.setCarDetectState(ConstantState.CAR_DETECT_STATE_FIRST_PASS);
		int carFirstDetectThrough = tvStatisRpcService.searchCarFirstDetectInfo(marketStorageVO);
		marketStorageVO.setCarDetectState(ConstantState.CAR_DETECT_STATE_FIRST_NG);
		int carFirstDetectNotThrough = tvStatisRpcService.searchCarFirstDetectInfo(marketStorageVO);
		res.put("carFirstDetectThrough", carFirstDetectThrough);
		res.put("carFirstDetectNotThrough", carFirstDetectNotThrough);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	/**
	 * 查询每日初检信息
	 * @param marketStorageVO
	 * @param dayNum
	 * @return
	 */
	@RequestMapping(value = "/getDayCarFirstDetectInfo")
	public ResultModel getDayCarFirstDetectInfo(MarketStorageVO marketStorageVO, Integer dayNum){
		Map<String, Object> res = new HashMap<>();
		if (!createMarketStorageVOStartAndEndDate(marketStorageVO, dayNum)) {
			return new ResultModel(ResultCode.RESULT_FAIL, "起止日期不能为空！");
		}
		List<MarketStorageVO> carFirstDetectInfo = tvStatisRpcService.searchDayCarFirstDetectInfo(marketStorageVO);
		res.put("carFirstDetectInfo", carFirstDetectInfo);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	/**
	 * 查询认证信息
	 * @param marketStorageVO
	 * @return
	 */
	@RequestMapping(value = "/getCarAuthInfo")
	public ResultModel getCarAuthInfo(MarketStorageVO marketStorageVO){
		Map<String, Object> res = new HashMap<>();
		//查询认证信息
		marketStorageVO.setCarAuthState(ConstantState.CAR_AUTH_STATE_AUTHED);
		int carOutAuthed = tvStatisRpcService.searchCarAuthNum(marketStorageVO);
		marketStorageVO.setCarAuthState(ConstantState.CAR_AUTH_STATE_INITIAL);
		int carOutNotAuthed = tvStatisRpcService.searchCarAuthNum(marketStorageVO);
		res.put("carOutAuthed", carOutAuthed);
		res.put("carOutNotAuthed", carOutNotAuthed);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	/**
	 * 查询出/入库品牌对比
	 * @param marketStorageVO
	 * @return
	 */
	@RequestMapping(value = "/getCarOutAndInBrandContrast")
	public ResultModel getCarOutAndInBrandRank(MarketStorageVO marketStorageVO){
		Map<String, Object> res = new HashMap<>();
		//查询认证信息
		List<MarketStorageVO> carOutAndInBrandContrast = tvStatisRpcService.searchCarOutAndInBrandContrast(marketStorageVO);
		res.put("carOutAndInBrandContrast", carOutAndInBrandContrast);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}
}
